<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        (function(){
            function Observer(){
                //保存多个消息类型和对应的回调函数
                this.obj = {};
                // {
                //     "lijiaqi":[ function(){}, function(){} , ... ]
                // }
                //订阅消息
                this.on = function( type , handler ){
                    if( this.obj[type] ){
                        this.obj[type].push(handler);
                    }else{
                        this.obj[type] = [handler];
                    }
                }

                //发布消息
                this.emit = function( type , msg ){
                    if( this.obj[type] ){
                        this.obj[type].forEach((item)=>{
                            item( msg );
                        })
                    }
                }
            }
            window.Observer = Observer;
        })()

        var os = new Observer()

        //订阅消息
        os.on('lijiaqi', function(msg){
            console.log('a 收到了消息:' + msg);
        })

        os.on('lijiaqi', function(msg){
            console.log('b 收到了消息:' + msg);
        })

        os.on('lijiaqi', function(msg){
            console.log('c 收到了消息:' + msg);
        })

        //发布消息
        os.emit('weiya','今晚吃鸡@!');
        
    </script>
</body>
</html>